home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / ibus / setup / enginetreeview.py < prev    next >
Text File  |  2009-11-05  |  8KB  |  235 lines

  1. # vim:set et sts=4 sw=4:
  2. #
  3. # ibus - The Input Bus
  4. #
  5. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this program; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20. # Boston, MA  02111-1307  USA
  21.  
  22. import gtk
  23. import glib
  24. import gobject
  25. import pango
  26. import ibus
  27.  
  28. from icon import load_icon
  29.  
  30. from gettext import dgettext
  31. _  = lambda a : dgettext("ibus", a)
  32. N_ = lambda a : a
  33.  
  34. class EngineTreeView(gtk.TreeView):
  35.     __gsignals__ = {
  36.         'changed' : (
  37.             gobject.SIGNAL_RUN_LAST,
  38.             gobject.TYPE_NONE,
  39.             ())
  40.     }
  41.  
  42.     def __init__(self, engines):
  43.         super(EngineTreeView, self).__init__()
  44.  
  45.         self.__engines = set([])
  46.         self.__changed = False
  47.  
  48.         # self.set_headers_visible(True)
  49.         self.set_reorderable(True)
  50.  
  51.         self.__model = gtk.ListStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
  52.         self.set_model(self.__model)
  53.         self.__model.connect("row-changed", self.__emit_changed_delay_cb, "row-changed")
  54.         self.__model.connect("row-deleted", self.__emit_changed_delay_cb, "row-deleted")
  55.         self.__model.connect("row-inserted", self.__emit_changed_delay_cb, "row-inserted")
  56.         self.__model.connect("rows-reordered", self.__emit_changed_delay_cb, "rows-reordered")
  57.  
  58.         # create im name & icon column
  59.         column = gtk.TreeViewColumn(_("Input Method"))
  60.         column.set_min_width(220)
  61.  
  62.         renderer = gtk.CellRendererPixbuf()
  63.         renderer.set_property("xalign", 0)
  64.         column.pack_start(renderer, False)
  65.         column.set_cell_data_func(renderer, self.__icon_cell_data_cb)
  66.  
  67.         renderer = gtk.CellRendererText()
  68.         renderer.set_property("xalign", 0)
  69.         renderer.set_property("ellipsize", pango.ELLIPSIZE_END)
  70.         column.pack_start(renderer, True)
  71.         column.set_cell_data_func(renderer, self.__name_cell_data_cb)
  72.         self.append_column(column)
  73.  
  74.         # create im keyboard layout column
  75.         renderer = gtk.CellRendererCombo()
  76.         model = gtk.ListStore(gobject.TYPE_STRING)
  77.         model.append(("us",))
  78.         model.append(("jp",))
  79.         model.append(("xkb",))
  80.         renderer.set_property("xalign", 0)
  81.         renderer.set_property("model", model)
  82.         renderer.set_property("text-column", 0)
  83.         renderer.set_property("has-entry", False)
  84.         renderer.set_property("editable", True)
  85.         renderer.connect("changed", self.__engine_layout_changed_cb)
  86.  
  87.         column = gtk.TreeViewColumn(_("Kbd"))
  88.         column.set_expand(False)
  89.         column.set_fixed_width(32)
  90.         column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
  91.         column.pack_start(renderer, False)
  92.         column.set_cell_data_func(renderer, self.__layout_cell_data_cb)
  93.         # self.append_column(column)
  94.  
  95.         self.set_engines(engines)
  96.  
  97.     def __emit_changed(self, *args):
  98.         if self.__changed:
  99.             self.__changed = False
  100.             self.emit("changed")
  101.  
  102.     def __emit_changed_delay_cb(self, *args):
  103.         if not self.__changed:
  104.             self.__changed = True
  105.             glib.idle_add(self.__emit_changed)
  106.  
  107.  
  108.     def __icon_cell_data_cb(self, celllayout, renderer, model, iter):
  109.         engine = self.__model.get_value(iter, 0)
  110.  
  111.         icon_size = gtk.icon_size_lookup(gtk.ICON_SIZE_LARGE_TOOLBAR)[0]
  112.         pixbuf = load_icon(engine.icon, gtk.ICON_SIZE_LARGE_TOOLBAR)
  113.  
  114.         if pixbuf == None:
  115.             pixbuf = load_icon("engine-default", gtk.ICON_SIZE_LARGE_TOOLBAR)
  116.         if pixbuf == None:
  117.             pixbuf = load_icon("gtk-missing-image", gtk.ICON_SIZE_LARGE_TOOLBAR)
  118.  
  119.         renderer.set_property("pixbuf", pixbuf)
  120.  
  121.     def __name_cell_data_cb(self, celllayout, renderer, model, iter):
  122.         engine = self.__model.get_value(iter, 0)
  123.         renderer.set_property("sensitive", True)
  124.         language = ibus.get_language_name(engine.language)
  125.         renderer.set_property("text", "%s - %s" % (language, engine.longname))
  126.         if self.__model.get_path(iter)[0] == 0:
  127.             # default engine
  128.             renderer.set_property("weight", pango.WEIGHT_BOLD)
  129.         else:
  130.             renderer.set_property("weight", pango.WEIGHT_NORMAL)
  131.  
  132.     def __layout_cell_data_cb(self, celllayout, renderer, model, iter):
  133.         engine = self.__model.get_value(iter, 0)
  134.         layout = self.__model.get_value(iter, 1)
  135.         renderer.set_property("sensitive", True)
  136.         if not layout:
  137.             layout = engine.layout
  138.         renderer.set_property("text", layout)
  139.         if self.__model.get_path(iter)[0] == 0:
  140.             #default engine
  141.             renderer.set_property("weight", pango.WEIGHT_BOLD)
  142.         else:
  143.             renderer.set_property("weight", pango.WEIGHT_NORMAL)
  144.  
  145.     def __engine_layout_changed_cb(self, combo, path, iter):
  146.         return
  147.         i = self.__model.get_iter(path)
  148.         layout = combo.get_property("model").get_value(iter, 0)
  149.         self.__model.set_value(i, 1, layout)
  150.  
  151.     def set_engines(self, engines):
  152.         self.__model.clear()
  153.         self.__engines = set([])
  154.         for e in engines:
  155.             if e in self.__engines:
  156.                 continue
  157.             iter = self.__model.append(None)
  158.             self.__model.set(iter, 0, e)
  159.             self.__engines.add(e)
  160.         self.__emit_changed()
  161.  
  162.     def get_selected_iter(self):
  163.         selection = self.get_selection()
  164.         if selection:
  165.             return selection.get_selected()[1]
  166.  
  167.     def get_engines(self):
  168.         engines = [ r[0] for r in self.__model if r[0] != None]
  169.         return engines
  170.  
  171.     def get_select_engine(self):
  172.         iter = self.get_selected_iter()
  173.         if iter == None:
  174.             return None
  175.         row = self.__model.get(iter, 0)
  176.         return row[0]
  177.  
  178.     def prepend_engine(self, engine):
  179.         if engine == None or engine in self.__engines:
  180.             return
  181.         iter = self.__model.prepend(None)
  182.         self.__model.set(iter, 0, engine)
  183.         self.__engines.add(engine)
  184.         self.scroll_to_cell(self.__model[0].path, None)
  185.  
  186.     def append_engine(self, engine):
  187.         if engine == None or engine in self.__engines:
  188.             return
  189.         iter = self.__model.append(None)
  190.         self.__model.set(iter, 0, engine)
  191.         self.__engines.add(engine)
  192.         self.scroll_to_cell(self.__model[-1].path, None)
  193.  
  194.     def remove_engine(self):
  195.         iter = self.get_selected_iter()
  196.         if iter == None:
  197.             return
  198.         row = self.__model[iter]
  199.         engine = row[0]
  200.         self.__engines.remove(engine)
  201.         index = row.path[0]
  202.         self.__model.remove(iter)
  203.         try:
  204.             row = self.__model[index]
  205.             selection = self.get_selection()
  206.             selection.select_path(row.path)
  207.         except:
  208.             pass
  209.  
  210.     def move_up_engine(self):
  211.         iter = self.get_selected_iter()
  212.         if iter == None:
  213.             return
  214.         row = self.__model[iter]
  215.         index = row.path[0]
  216.         if index == 0:
  217.             return
  218.         self.__model.swap(iter, self.__model[index - 1].iter)
  219.         self.scroll_to_cell(row.path, None)
  220.  
  221.     def move_down_engine(self):
  222.         iter = self.get_selected_iter()
  223.         if iter == None:
  224.             return
  225.         row = self.__model[iter]
  226.         index = row.path[0]
  227.         last_row = self.__model[-1]
  228.         last_index = last_row.path[0]
  229.         if index == last_index:
  230.             return
  231.         self.__model.swap(iter, self.__model[index + 1].iter)
  232.         self.scroll_to_cell(row.path, None)
  233.  
  234. gobject.type_register(EngineTreeView)
  235.